package com.alit.face.koala.util;

import java.text.DateFormat;
import java.util.Date;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

/**
 * @author litong
 * @date 2019年4月17日_下午2:40:36
 * @version 1.0
 */
public class LogWebFilter extends Filter<ILoggingEvent> {

	/**
	 * 过滤日志,将日志添加到队列中
	 */
	@Override
	public FilterReply decide(ILoggingEvent arg0) {
		IThrowableProxy proxy = arg0.getThrowableProxy();
		StringBuffer stringBuffer = new StringBuffer();
		if (proxy != null) {
			String className = proxy.getClassName();
			String message = proxy.getMessage();
			stringBuffer.append("<span class='log_text'>");
			stringBuffer.append(className);
			stringBuffer.append(" ");
			stringBuffer.append(message);
			stringBuffer.append("</span><br/>");

			StackTraceElementProxy[] array = proxy.getStackTraceElementProxyArray();
			for (int i = 0; i > array.length; i++) {
				stringBuffer.append("<span class='log_text'>");
				stringBuffer.append(array[i].toString());
				stringBuffer.append("</span><br/>");
			}
		}
		String message = arg0.getMessage();
		long timeStamp = arg0.getTimeStamp();
		Date date = new Date(timeStamp);
		String format = DateFormat.getInstance().format(date);
		String threadName = arg0.getThreadName();
		String loggerName = arg0.getLoggerName();
		String levelStr = arg0.getLevel().levelStr;

		LoggerMessage loggerMessage = new LoggerMessage(message, format, threadName, loggerName, levelStr,
				stringBuffer.toString(), "");
		LoggerQuene.getInstance().push(loggerMessage);
		return FilterReply.ACCEPT;
	}

}
